aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docs/pages/api/user/[id].tsx
blob: 091d71617b0947b627af89863fa9df2afa2e5d00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { NextApiRequest, NextApiResponse } from "next";
import { withSentry } from "@sentry/nextjs";
import {
  getSubscriber,
  Subscriber,
  updateSubscriber,
} from "../../../lib/ConvertKitApi";

async function handler(req: NextApiRequest, res: NextApiResponse) {
  try {
    if (req.method === "PUT") {
      const subscriber = await updateSubscriber(
        req.query.id as string,
        {
          first_name: req.body.first_name,
          email_address: req.body.email_address,
          fields: req.body.fields,
        } as Subscriber
      );
      res.setHeader("Content-Type", "application/json");
      res.statusCode = 204;
      res.json(subscriber);
    } else {
      const subscriber = await getSubscriber(req.query.id as string);
      res.setHeader("Content-Type", "application/json");
      res.statusCode = 200;
      res.json(subscriber);
    }
  } catch (error) {
    console.log(error);
    res.statusCode = 500;
    res.json({ okay: false });
  }
}

export default withSentry(handler);